package com.hundsun.model;

import com.alibaba.fastjson.JSON;
import com.hundsun.model.interfaces.ConfigInfo;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jiangwj20966 2018/9/21
 */
public class MappingConfigInfo implements ConfigInfo {
    private String tableName;
    private String excelFileName;

    private List<ColumnInfo> columnInfos;

    private Map<String, ColumnInfo> mappingInfo;

    public MappingConfigInfo() {
        columnInfos = new ArrayList<>();
    }

    public void addColumnInfo(ColumnInfo columnInfo) {
        columnInfos.add(columnInfo);
    }

    @Override
    public String getColumnNameByExcelColumnIndex(String excelColumnIndex) {
        remappingInfo();
        return mappingInfo.get(excelColumnIndex).columnName;
    }

    private void remappingInfo() {
        if (mappingInfo == null) {
            mappingInfo = new HashMap<>();
        }
        if (columnInfos.size() != mappingInfo.size()) {
            for (ColumnInfo columnInfo : columnInfos) {
                if (!mappingInfo.containsKey(columnInfo.getExcelColumnIndex())) {
                    mappingInfo.put(columnInfo.getExcelColumnIndex(), columnInfo);
                }
            }
        }
    }


    @Override
    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        if (StringUtils.isBlank(tableName)) {
            throw new IllegalArgumentException("表名不能为空");
        }
        this.tableName = tableName;
    }

    @Override
    public String getExcelFileName() {
        return excelFileName;
    }

    public void setExcelFileName(String excelFileName) {
        if (StringUtils.isBlank(excelFileName)) {
            throw new IllegalArgumentException("excel名不能为空");
        }
        this.excelFileName = excelFileName;
    }

    public List<ColumnInfo> getColumnInfos() {
        return Collections.unmodifiableList(columnInfos);
    }

    public static class ColumnInfo {
        private String columnName;
        private String excelColumnIndex;

        public ColumnInfo(String columnName, String excelColumnIndex) {
            if (StringUtils.isBlank(columnName) || StringUtils.isBlank(excelColumnIndex)) {
                throw new IllegalArgumentException("表字段和Excel字段不能为空");
            }
            this.columnName = columnName;
            this.excelColumnIndex = excelColumnIndex;
        }

        public ColumnInfo() {
        }

        public String getColumnName() {
            return columnName;
        }

        public void setColumnName(String columnName) {
            this.columnName = columnName;
        }

        public String getExcelColumnIndex() {
            return excelColumnIndex;
        }

        public void setExcelColumnIndex(String excelColumnIndex) {
            this.excelColumnIndex = excelColumnIndex;
        }
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
